package com.person.jc.tag;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

/**
 * 所有获取数据的方法都必须以get*命名,否则该方法将不会被查找
 */
public class SelectTagDao {

	public final static SelectTagDao me = new SelectTagDao();
	
    private Map<String, String> sqlMap = new HashMap<String, String>();
    
    public SelectTagDao() {
        setSqlMap(sqlMap);
    }

    // 将properties的信息放入map
    public static void setSqlMap(Map<String, String> sqlMap) {
        try {
            Enumeration<?> en = PropKit.use("config/selectTag.properties").getProperties().keys();
            while (en.hasMoreElements()) {
                String key = (String) en.nextElement();
                String property = PropKit.use("config/selectTag.properties").get(key);
                sqlMap.put(key, property);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据属性名字取出sql并根据解析出来的列名生成新的sql并返回结果
     */
    public List<Record> getExecuteResult(String sqlname, String sqlparam) {
        String sql = null;
        if (sqlMap.containsKey(sqlname)) {
            sql = sqlMap.get(sqlname);
        }
        else {
            return null;
        }
        
        String[] params = null;
        if (sqlparam == null) {
            params = new String[]{};
        }
        else
        {
            params = sqlparam.split(",");
        }
 
        return this.executeSQL(sql, params);
    }

    /**
     * 执行查询并返回结果,这是一个公共方法
     */
    public List<Record> executeSQL(String sql, Object[] params) {
        return Db.find(sql, params);
    }

}
